package com.xxx.zkconfig.helper;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.curator.framework.recipes.cache.TreeCache;
import org.apache.curator.framework.recipes.cache.TreeCacheListener;

/**
 * 对path路径下所有孩子节点的监听
 * TreeCache treeCache=new TreeCache(CuratorUtils.getZkClient(), path);
 * treeCache.start();
 * treeCache.getListenable().addListener(new TreeCacheListener() {
 *
 * @Override public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent) throws Exception {
 * System.out.println("tree node changed "+treeCacheEvent.getType());
 * <p>
 * }
 * });
 */
public class NodeChangeListenerUtils {


    public static void registerPathChildrenCacheListener(String path, TreeCacheListener pathChildrenCacheListener) {
        registerPathChildrenCacheListener(ZkUtils.getZkClient(), path, pathChildrenCacheListener);

    }


    public static void registerPathChildrenCacheListener(CuratorFramework zkCliect
            , String path, TreeCacheListener pathChildrenCacheListener) {
        try {
            //子节点的监听
            TreeCache cache = new TreeCache(zkCliect, path);
            cache.start();
            //注册监听
            cache.getListenable().addListener(pathChildrenCacheListener);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
